import Shop.damain.Users;
import Shop.mappers.UsersMapper;
import Shop.service.UserService;
import Shop.tools.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by BadHuge on 2020/10/2.
 */
public class Register extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        SqlSession sqlSession = MybatisUtil.getSqlSession();

        UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
        UserService us = new UserService();
        Users user = new Users(username,password,0,"11",1,"11");
        int i = usersMapper.addUsers(user);
        //非查询查询操作必须手动提交
        sqlSession.commit();

        MybatisUtil.closeSqlSession();
     /*   boolean i =us.register(username,password);*/
        if (i==1)
        response.sendRedirect("login.jsp");
        else
            response.sendRedirect("register.jsp");
    }
}
